package com.zhuiyun.project.api.venuemanagement.service.impl;

import com.zhuiyun.project.api.venuemanagement.entity.BanRecord;
import com.zhuiyun.project.api.venuemanagement.entity.SysDept;
import com.zhuiyun.project.api.venuemanagement.entity.SysUser;
import com.zhuiyun.project.api.venuemanagement.entity.VenueManagement;
import com.zhuiyun.project.api.venuemanagement.mapper.VenueReviewMapper;
import com.zhuiyun.project.api.venuemanagement.model.VenueAddModel;
import com.zhuiyun.project.api.venuemanagement.model.VenueSelectModel;
import com.zhuiyun.project.api.venuemanagement.service.VenueReviewService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.UploadFileUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.uuid.IdUtils;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @ClassName VenueReviewServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/12/6 10:55
 **/
@Service
public class VenueReviewServiceImpl implements VenueReviewService {
    @Autowired
    VenueReviewMapper venueReviewMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    /*
     * @Author wuwan
     * @Description 添加场馆信息
     * @Date 2023/11/2 14:04
     * @Return
     */
    @Override
    @Transactional
    public CommonResult addVenueReview(VenueAddModel venueAddModel, MultipartFile file) throws CommonException {
        if (ValidatorUtil.ValidationObject(venueAddModel)){
            // 生成场馆唯一编号
            String s1 = IdUtils.randomUUID();
            // 手机号等同于账号
            String venuePhone = venueAddModel.getPhone();
            // 给手机号加密当做密码使用
            String encode = bCryptPasswordEncoder.encode(venuePhone);
            // 上传图片到服务器
            String s = UploadFileUtils.uploadFile(file);
            //添加账号密码到登录表
            SysUser tenantUser = new SysUser();
            tenantUser.setUserName(venueAddModel.getUserName());
            tenantUser.setPhone(venuePhone);
            tenantUser.setLoginName(venuePhone);
            tenantUser.setPassword(encode);
            tenantUser.setAvatarId(Integer.parseInt(s));
            tenantUser.setEmail(venueAddModel.getEmail());
            tenantUser.setVenueUniqueIdentifier(s1);
            tenantUser.setStatus("2");
            tenantUser.setDelFlag("0");
            tenantUser.setCreateTime(new Date());
            tenantUser.setUpdateTime(new Date());
            // 添加部门表 （添加新用户自动分配部门）
            SysDept sysDept = new SysDept();
            sysDept.setVenueUniqueIdentifier(s1);
            sysDept.setDeptName(venueAddModel.getUserName());
            sysDept.setLeader(venueAddModel.getVenueHeadName());
            sysDept.setPhone(venuePhone);
            sysDept.setEmail(venueAddModel.getEmail());
            // 部门添加
            venueReviewMapper.addDepartment(sysDept);
            tenantUser.setDeptId(sysDept.getDeptId());
            // 用户表添加
            Integer user = venueReviewMapper.addSysUser(tenantUser);
            if (user < 1) {
                return CommonResult.error("账号创建失败");
            }
            VenueManagement venueManagement = new VenueManagement();
            // addModel 对象拷贝成 sysUser
            BeanUtils.copy(venueAddModel, venueManagement);
            // 生成随机数 当做店铺唯一标识符
            venueManagement.setVenueUniqueIdentifier(s1);
            Integer integer = venueReviewMapper.addVenueReview(venueManagement);
            if (integer >= 1) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else {
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    /*
     * @Author wuwan
     * @Description 查询所有的未授权的场馆数据
     * @Date 2023/12/6 11:03
     * @Return
     */
    @Override
    public CommonResult selectVenueReview(PageRequest pageRequest, String userName, String phone) {
        MybatisPageHelper.startPage(pageRequest);
        List<VenueSelectModel> venueManagements = venueReviewMapper.selectVenueReview(userName, phone);
        PageResult pageResult = MybatisPageHelper.getPageResult(venueManagements);
        return CommonResult.ok(pageResult);
    }
    /*
    * @Author wuwan
    * @Description 授权操作
    * @Date 2023/12/9 15:37
    * @Return
    */
    @Override
    public CommonResult upVenueReview(BanRecord banRecord) {
        Integer integer = venueReviewMapper.upVenueReview(banRecord);
        if (integer >= 1){
            return CommonResult.ok("授权成功");
        }
        return CommonResult.error("授权失败");
    }
}
